package cn.itcast.order.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.itcast.order.model.OrderInfo;
import cn.itcast.order.service.IOrderInfoService;
import cn.itcast.order.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @program: springcloud-demo2
 * @description:
 * @author: sch
 * @create: 2023-04-02 12:03
 **/
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderInfoService orderService;

    @RequestMapping("hello")
    public String hello() {
        return "hello order";
    }

    @RequestMapping("addOrder")
    public String addOrder(OrderInfoVo orderInfoVo) {
        if (Objects.isNull(orderInfoVo)) {
            System.out.println("order  ===> addOrder...入参空");
            return null;
        }
        OrderInfo orderInfo = BeanUtil.copyProperties(orderInfoVo, OrderInfo.class);
        int count = orderService.add(orderInfo);
        System.out.println("order  ===> addOrder...插入成功" + count);
        return "addOrder success";
    }

    @RequestMapping("addOrder2")
    public String addOrder2(@RequestBody OrderInfoVo orderInfoVo) {
        if (Objects.isNull(orderInfoVo)) {
            System.out.println("order  ===> addOrder...入参空");
            return null;
        }
        OrderInfo orderInfo = BeanUtil.copyProperties(orderInfoVo, OrderInfo.class);
        int count = orderService.add(orderInfo);
        System.out.println("order  ===> addOrder...插入成功" + count);
        return "addOrder success";
    }

    @RequestMapping("orderList")
    public List<OrderInfo> orderList() {
        List<OrderInfo> orderInfos = orderService.listOrder();
        if (CollUtil.isEmpty(orderInfos)) {
            System.out.println("未查到数据....");
        }
        System.out.println("order.orderList查询成功" + orderInfos);

        return orderInfos;
    }

    @RequestMapping("findOrderByUserId")
    public List<OrderInfo> findOrderByUserId(Long userId) {
        List<OrderInfo> orderInfos = orderService.findOrderByUserId(userId);
        if (CollUtil.isEmpty(orderInfos)) {
            System.out.println("未查到数据....");
        }

        System.out.println("order.findOrderByUserId查询成功" + orderInfos);
        return orderInfos;
    }
}
